package com.baichang.taoshunda.controller.mobile.business;

import com.baichang.framework.controller.BaseMobileController;
import com.baichang.framework.controller.IService;
import com.baichang.framework.excelption.ServiceException;
import com.baichang.taoshunda.controller.ControllerConstants;
import com.baichang.taoshunda.service.BusinessMoneyRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.text.ParseException;

/**
 * Created by Administrator on 2018/2/23 0023.
 */
@Controller
@RequestMapping(ControllerConstants.DEPOSIT)
public class BusinessDepositMController extends BaseMobileController {

    @Autowired
    private BusinessMoneyRecordService businessMoneyRecordService;

    /**
     * 保证金查询
     *
     * @param request
     * @param response
     */
    @RequestMapping("/queryTypeMoney")
    public void queryTypeMoney(HttpServletRequest request, HttpServletResponse response) {
        initEncode(request, response, new IService() {
            @Override
            public Object init(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, String json) throws ServiceException {
                Integer busId = getInt(json, "busId", 0);
                Integer typeId = getInt(json, "typeId", 0);
                Integer is = getInt(json, "is", 0);
                return businessMoneyRecordService.queryTypeMoney(busId, typeId, is);
            }
        });
    }

    /**
     * 支付接口
     *
     * @param request
     * @param response
     */
    @RequestMapping("/payDeposit")
    public void payDeposit(HttpServletRequest request, HttpServletResponse response) {
        initEncode(request, response, new IService() {
            @Override
            public Object init(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, String s) throws ServiceException {
                Integer busId = getInt(s, "busId", 0);
                Integer payType = getInt(s, "payType", 0);
                BigDecimal money = BigDecimal.valueOf(getDouble(s, "money", 0));
                return businessMoneyRecordService.payDeposit(busId, payType, money);
            }
        });
    }

    /**
     * 支付成功调用
     *
     * @param request
     * @param response
     */
    //TODO  商家端支付回调
    @RequestMapping("/successPayDeposit")
    public void successPayDeposit(HttpServletRequest request, HttpServletResponse response) {
        initEncode(request, response, new IService() {
            @Override
            public Object init(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, String s) throws ServiceException {
                Integer busId = getInt(s, "busId", 0);
                Integer payType = getInt(s, "payType", 0);
                Integer type = getInt(s, "type", 0);
                BigDecimal money = BigDecimal.valueOf(getDouble(s, "money", 0));
                return businessMoneyRecordService.successPayDeposit(busId, payType, money, type);
            }
        });
    }

    /**
     * 退换保证金
     *
     * @param request
     * @param response
     */
    @RequestMapping("/returnDepositMoney")
    public void returnDepositMoney(HttpServletRequest request, HttpServletResponse response) {
        initEncode(request, response, new IService() {
            @Override
            public Object init(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, String s) throws ServiceException {
                Integer id = getInt(s, "id", 0);
                BigDecimal money = BigDecimal.valueOf(getDouble(s, "money", 0));
                Integer busId = getInt(s, "busId", 0);
                Integer type = getInt(s, "type", 0); //1.假一罚三  2.消费者
                try {
                    return businessMoneyRecordService.returnDepositMoney(id, busId, money, type);
                } catch (ParseException e) {
                    e.printStackTrace();
                }
                return null;
            }
        });
    }
}
